package com.zjp.mg.controller;


import cn.hutool.core.util.RandomUtil;
import com.zjp.mg.core.BaseController;
import com.zjp.mg.core.AjaxResult;
import com.zjp.mg.model.Apple;
import com.zjp.mg.model.Order;
import com.zjp.mg.model.OrderSchemeDto;
import com.zjp.mg.service.OrderSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

    @Autowired
    OrderSchemeService orderSchemeService;

    @PostMapping("addSchemeOrder")
    public AjaxResult addOrder(@RequestBody OrderSchemeDto dto) {
        Order order = orderSchemeService.addOrder(dto);
        return AjaxResult.success(orderSchemeService.getOrder(Long.valueOf(order.getOrderId())));
    }


    @GetMapping("/{orderId}")
    public AjaxResult getOrder(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(orderSchemeService.getOrder(orderId));
    }

    @DeleteMapping("/{orderId}")
    public AjaxResult deleteOrder(@PathVariable("orderId") Long orderId) {
        orderSchemeService.deleteOrder(orderId);
        return AjaxResult.success();
    }

    @PutMapping("/updateOrder")
    public AjaxResult updateOrder(@RequestBody OrderSchemeDto dto) {
        return AjaxResult.success(orderSchemeService.updateOrder(dto));
    }

    @GetMapping("/pageList")
    public AjaxResult pageList() {
        return AjaxResult.success(orderSchemeService.pageList(1, 100));
    }

    @GetMapping("/group")
    public AjaxResult group() {
        orderSchemeService.group();
        return AjaxResult.success();
    }

    @GetMapping("/transaction")
    public AjaxResult transaction() {
        Apple apple = new Apple();
        apple.setName(RandomUtil.randomString(5));

        orderSchemeService.transaction(apple);
        return AjaxResult.success();
    }

    @GetMapping("/transactionV2")
    public AjaxResult transactionV2() {
        Apple apple = new Apple();
        apple.setName(RandomUtil.randomString(5));
        orderSchemeService.transactionV2(apple);
        return AjaxResult.success();
    }


}
